package com.zhonghui.common.util;

import com.zhonghui.common.eshopVo.CityVo;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class CityTreeUtil {
    /**
     * static List<CityVo> list2TreeConverter --->  returnList res
     * static void recursionFn ----> nextChild  CollectionUtils.isEmpty
     * static List<CityVo> ---> childList t1
     */

    public static List<CityVo> list2TreeConverter(List<CityVo> list, int parentId){
        ArrayList<CityVo> returnList = new ArrayList<>();
        for (CityVo res : list) {
            if (res.getPid() == parentId){
                recursionFn(list,res);

                returnList.add(res);
            }
        }
        return  returnList;
    }

    public static void recursionFn(List<CityVo> list, CityVo t){
        List<CityVo> childList = getChildList(list, t);
        if (childList.size()  == 0){
            t.setChildren(null);
        }else {
            t.setChildren(childList);
        }

        for (CityVo nextChild : childList) {
            if (!CollectionUtils.isEmpty(childList)){
                recursionFn(list,nextChild);
            }
        }
    }

    public static List<CityVo> getChildList(List<CityVo> list,CityVo t){
        ArrayList<CityVo> childList = new ArrayList<>();
        for (CityVo t1 : list) {
            if (t1.getPid().equals(t.getValue())){
                childList.add(t1);
            }
        }

        return childList;
    }
}

